﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TVShow.Framework;

namespace TVShow
{
  public partial class MainWindow : Form, IPlugin
  {
    public MainWindow() {
      InitializeComponent();
    }

    /// <summary>
    /// when true, media panel should fill the main window.
    /// when false, media panel is in lower-left portion of screen.
    /// </summary>
    bool mediaZoom = false;
    public bool MediaZoom {
      get { return mediaZoom; }
      set {
        mediaZoom = value;
        MainWindow_Resize(this, new EventArgs());
      }
    }

    PositionManager PositionManager;

    DisplayManager display = new DisplayManager();
    private void MainWindow_Load(object sender, EventArgs e) {
      display.MediaPanel = new DisplayItem(MediaPanel);
      display.MenuPanel = new DisplayItem(Menupanel);
      display.SideBar = new DisplayItem(SideBar);
      display.PowerBar = new DisplayItem(PowerBar);
      display.Header = new DisplayItem(HeaderLabel);
      display.TransportPanel = new DisplayItem(TransportPanel);
      Show();

      PositionManager = new PositionManager(this);
      PositionManager.Add(TransportPanel);
      PositionManager.Add(MediaPanel);
      PositionManager.Add(Menupanel);
      PositionManager.Add(SideBar);
      PositionManager.Add(HeaderLabel);
      PositionManager.Add(Title);

      Application.DoEvents();
      LoadPlugin(display);

      PluginManager.LoadPlugins(display);

      TVSMessage.Send("showmenu;Main Menu");
      TVSMessage.MessageFinished += new TVSMessage.MessageEventHandler(TVSMessage_MessageFinished);
    }

    void TVSMessage_MessageFinished(TVSMessage message) {
      if (message.Text == "shutdown")
        ShutDown();
    }

    #region IPlugin Members


    public void LoadPlugin(DisplayManager Display) {
      TVSMessage.RegisterPlugin(this);
    }

    public void ProcessMessage(TVShow.Framework.TVSMessage Event) {
      switch (Event.Text) {
        default:
          break;
      }
    }

    public void ShutDown() {
      DialogResult = DialogResult.OK;
      Close();
    }

    #endregion

    private void ExitButton_Click(object sender, EventArgs e) {
      TVSMessage.Send("shutdown");
    }

    private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) {
      if (DialogResult != DialogResult.OK) {
        TVSMessage.Send("shutdown");
        e.Cancel = true;
      }
    }

    private void MainWindow_KeyDown(object sender, KeyEventArgs e) {
      if (display.Keyboard != null)
        display.Keyboard.KeyDown(e);
    }

    private void MainWindow_KeyUp(object sender, KeyEventArgs e) {
      if (display.Keyboard != null)
        display.Keyboard.KeyUp(e);
    }

    RectangleF Border = new RectangleF(.1f, .1f, .9f, .9f);
    private void MainWindow_Resize(object sender, EventArgs e) {
      PositionManager.Resize();
      if (mediaZoom)
        MediaPanel.Bounds = this.ClientRectangle;
    }


  }
}
